<?php

namespace app\api\controller;

use app\api\model\Send;
use app\common\library\wechat\WxPay;
use think\facade\Db;
use think\Response;


class Time extends Wap
{



    /**
     * 商家后台自动关灯
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function door_list_data()
    {
        $list = Db::name('coutomer_door_list_data')->where('is_status', '=', 0)->select();
        foreach ($list as $i => $v) {
            if ($v['end_time'] <= time()) {
                $door = Db::name('customer_door_list')->where('mark', 1)->where('door_id', $v['door_id'])->find();
                if(!$door){
                    continue ;
                }
                $course = Db::name('customer_course')->where('mark', 1)->where('door_id', $door['id'])->find();
                if(!$course){
                    continue ;
                }
                $order = Db::name('customer_order')->where('seat_id', $course['id'])->where('status', 2)->find();
                //没有订单
                if (!$order) {
                    $res = $this->newopen($door['door_id'], 4);
                    if($res['code']){
                        $status['is_status'] = 1;
                        Db::name('coutomer_door_list_data')->where('id', $v['id'])->update($status);
                    }

                } else {
                    $status['is_status'] = 1;
                    Db::name('coutomer_door_list_data')->where('id', $v['id'])->update($status);

                }

            }
        }
    }

    /**
     * 保洁自动关灯
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function Cleaning()
    {
        $list = Db::name('customer_order_cleaning')->where('status', [0, 1, 2])->where('add_time', '<', time())->where('end_time', '>', time())->select();
        foreach ($list as $i => $v) {
            if ($v['end_time'] < time()) {

                $order = Db::name('customer_order')->where('order_on', $v['order_on'])->find();
                if ($order) {
                    $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
                    $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                } else {
                    $order = Db::name('customer_seat_order')->where('order_on', $v['order_on'])->find();
                    $house_info = Db::name('customer_house')->where('id', $v['house_id'])->find();
                    $door = Db::name('customer_door_list')->where('id', $house_info['door_id'])->find();
                }
                $stauts['status'] = 4;
                $appkey = Db::name('customer_set_cabinet')->where('store_id', $v['store_id'])->find();
                $door_list_data = $appkey;
                $door_list_data['equipment_id'] = $door['door_id'];
                Db::name('customer_order_cleaning')->where('id', $v['id'])->update($stauts);
                $res = $this->newopen($door['door_id'], 4);

            }
        }
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function CourseOrder()
    {
        $list = Db::name('customer_order')->where('order_type', 1)->where('status', '=', 0)->select();
        foreach ($list as $i => $v) {
            $time = $v['add_time'] + 5 * 60;
            if ($time <= time()) {
                Db::name('customer_order')->where('id', $v['id'])->delete();
            }

        }
    }

    /**
     * 自动退押金
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function seatOrder()
    {
        $list = Db::name('customer_seat_order')->where('status', 1)->select();

        foreach ($list as $i => $v) {
            if ($v['end_time'] <= time()) {


                $wxapp = Db::name('mini_match')->where('id', 1)->find();
                $room = Db::name('coutomer_make')->where('store_id', $v['store_id'])->find();
                $wxConfig = array(
                    'app_id' => $wxapp['app_id'],
                    'app_key' => $wxapp['key'],
                    'mchid' => $wxapp['wc_hid'],
                    'app_secret' => $wxapp['api_key'],
                    'apikey' => $wxapp['api_key'],
                    'apiclient_url' => $wxapp['apiclient_url'],
                    'apiclient_key_url' => $wxapp['apiclient_key_url']
                );
                $WxPay = new WxPay($wxConfig);
                if ($v['sign_time'] > ($v['add_time'] + $room['time'] * 60) || $v['sign'] == 0) {
                    $userOrder = Db::name('')->where('seat_id', $v['seat_id'])->where('end_time', $v['add_time'])->where('add_time', $v['add_time'])->where('sign_time', '<=', $v['add_time'])->where('happy_id', $v['happy_id'])->where('method_id', $v['method_id'])->select();
                    foreach ($userOrder as $k => $m) {
                        $price = $v['price'] * 0.15;

                        if ($WxPay->refund($v['order_on'], $v['price'], $price) == true) {

                            $s['status'] = 4;
                            $s['retreat_price'] = $price;
                            $s['retreat_time'] = time();
                            Db::name('customer_seat_order')->where('order_on', $v['order_on'])->update($s);
                        } else {
                            $s['status'] = 3;
                            $s['retreat_price'] = 0;
                            $s['retreat_time'] = time();
                            Db::name('customer_seat_order')->where('order_on', $v['order_on'])->update($s);
                        }
                    }
                } elseif ($v['sign_time'] > $v['add_time'] && $v['sign_time'] < ($v['add_time'] + $room['time'] * 60)) {

                    $price = $v['price'] * 0.75 - $room['retreat_price'] * round((($v['sign_time'] - $v['add_time']) / 60), 2);


                    if ($WxPay->refund($v['order_on'], $v['price'], $price) == true) {

                        $s['status'] = 4;
                        $s['retreat_price'] = $price;
                        $s['retreat_time'] = time();
                        Db::name('customer_seat_order')->where('order_on', $v['order_on'])->update($s);
                    } else {
                        $s['status'] = 3;
                        $s['retreat_price'] = 0;
                        $s['retreat_time'] = time();
                        Db::name('customer_seat_order')->where('order_on', $v['order_on'])->update($s);
                    }
                } elseif ($v['sign_time'] <= $v['add_time']) {
                    $price = $v['price'] * 0.75;
                    $WxPay->refund($v['order_on'], $v['price'], $price);

                    $price = $v['price'];
                    if ($WxPay->refund($v['order_on'], $v['price'], $price) == true) {

                        $s['status'] = 4;
                        $s['retreat_price'] = $price;
                        $s['retreat_time'] = time();
                        Db::name('customer_seat_order')->where('order_on', $v['order_on'])->update($s);
                    } else {
                        $s['status'] = 3;
                        $s['retreat_price'] = 0;
                        $s['retreat_time'] = time();
                        Db::name('customer_seat_order')->where('order_on', $v['order_on'])->update($s);
                    }
                }
            }
        }
    }

    /**
     * 订单开始
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addOrder()
    {
        $list = Db::name('customer_order')->where('order_type', 1)->where('status', '=', 1)->select();
        foreach ($list as $i => $v) {
            if ($v['add_time'] <= time()) {
                $stauts['status'] = 2;
                $stauts['update_time'] = time();
                Db::name('customer_order')->where('id', $v['id'])->update($stauts);
            }

        }
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addRenewalOrder()
    {
        //   $list = Db::name('customer_renewal_order')->where('order_type', 1)->where('status', '=', 1)->select();
        $list = Db::name('customer_renewal_order')->where('status', '=', 1)->select();

        foreach ($list as $i => $v) {
            if ($v['add_time'] <= time()) {
                $stauts['status'] = 2;
                $stauts['update_time'] = time();
                Db::name('customer_renewal_order')->where('order_id', $v['order_id'])->update($stauts);

                if ($v['pay_type'] == 1) {
                    $this->distribution($v['store_id'], $v['pay_price'], 2, '加时订单', $v['order_id'],$v['order_on'],$v['transaction_id']);
                }
            }

        }
    }

    /**续单结束
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function endRenewalOrder()
    {
        // $list = Db::name('customer_renewal_order')->where('order_type', 1)->where('status', '=', 1)->select();
        $list = Db::name('customer_renewal_order')->where('status', '=', 2)->select();

        foreach ($list as $i => $v) {
            if ($v['end_time'] <= time()) {
                $stauts['status'] = 4;
                $stauts['update_time'] = time();
                Db::name('customer_renewal_order')->where('order_id', $v['order_id'])->update($stauts);
            }

        }
    }

    /**
     * 订单结束
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function endOrder()
    {
        $list = Db::name('customer_order')->where('status', '=', 2)->select();
        foreach ($list as $i => $v) {
            if ($v['end_time'] <= time()) {
                $course = Db::name('customer_course')->where('id', $v['seat_id'])->find();
                $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                if ($door) {
                    $stauts['status'] = 4;
                    $appkey = Db::name('customer_set_cabinet')->where('store_id', $v['store_id'])->find();
                    $door_list_data = $appkey;
                    $door_list_data['equipment_id'] = $door['door_id'];

                    if ($door['door_id'] != "") {
                        $res = $this->newopen($door['door_id'], 4);
                        if ($res['code'] == '200') {
                            Db::name('customer_order')->where('id', $v['id'])->update($stauts);
                            // 记录信息
                            $doorList = array(
                                'admin_id' => $v['store_id'],
                                'create_time' => time(),
                                'user_id' => $v['user_id'],
                                'status' => 1,
                                'order_on' => $v['order_on'],
                                'door_id' => $door['door_id'],
                                'type' => 1,
                            );
                            Db::name('mini_admin_lamp_list')->insert($doorList);
                        }
                    }
                } else {
                    $stauts['status'] = 4;
                    Db::name('customer_order')->where('id', $v['id'])->update($stauts);
                    $this->integral($v['store_id'], $v['pay_price'], $v['user_id'], $v['seat_id'], $v['order_on']);
                }
                $count = "预约包厢";
                $this->integral($v['store_id'], $v['price'], $v['user_id'], $v['seat_id'], $v['order_on']);
                if ($v['pay_type'] == 1) {
                    $this->distribution($v['store_id'], round($v['pay_price']-$v['jia_price'],2), 1, $count, $v['id'],$v['order_on'],$v['transaction_id']);
                }
                if (($v['pay_type'] == 3||$v['pay_type']==4)&&$v['pay_price']>0) {
                    $count = "抖音美团预约包厢";
                    $this->distribution($v['store_id'], round($v['pay_price']-$v['jia_price'],2), 1, $count, $v['id'],$v['order_on'],$v['transaction_id']);
                }

            } else {
                //保洁提醒
                if (600 + time() > $v['end_time']) {
                    if ($v['is_cleaning_end'] != 1) {
                        $s['is_cleaning_end'] = 1;
                        Db::name('customer_order')->where('order_on', '=', $v['order_on'])->update($s);
                        $this->send($v['id'], 1);
                    }
                }
                /** 语音结束播报5分账 */
                if ($v['is_voice_end'] != 1) {
                    if (time() + 300 > $v['end_time']) {
                        $seat_list = DB::name('customer_course')->where(['id' => $v['seat_id']])->find();
                        $door = DB::name('customer_door_list')->where(['id' => $seat_list['door_id']])->find();
                        $res = $this->newopen($door['door_id'], 3);
                        if($res['code']=='200'){
                            $d['is_voice_end'] = 1;
                            $d['update_time'] = time();
                            Db::name('customer_order')->where('order_on', $v['order_on'])->update($d);
                        }



                    }

                }

                /** 语音播报30分钟 */
                if ($v['is_voice'] != 1) {
                    $seat_list = DB::name('customer_course')->where(['id' => $v['seat_id']])->find();
                    $voice = 03;
                    $end_time = time() + 30 * 60;
                    if ($end_time > $v['end_time']) {
                        $door = DB::name('customer_door_list')->where(['id' => $seat_list['door_id']])->find();

                        if ($door) {
                            $res = $this->newopen($door['door_id'], 2);
                            if($res['code']=='200'){
                                $d['is_voice'] = 1;
                                $d['update_time'] = time();
                                Db::name('customer_order')->where('order_on', $v['order_on'])->update($d);
                            }
                        }
                    }
                }
            }

        }
    }


    public function delete()
    {
        $list = Db::name('customer_order')->where('status', 0)->select();
        foreach ($list as $i => $v) {
            if (($v['create_time'] + 300) < time()) {

                Db::name('customer_order')->where('id', $v['id'])->delete();
                Db::name('customer_order_cleaning')->where('order_on', $v['order_on'])->delete();
            }
        }
    }

    /**
     * 玩法开始
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function seatAddOrder()
    {
        $list = Db::name('customer_seat_order')->where('status', 1)->select();
        foreach ($list as $i => $v) {
            if ($v['add_time'] <= time()) {
                $status['status'] = 2;

                Db::name('customer_seat_order')->where('id', $v['id'])->update($status);
            }
        }
    }


    /**
     * @param $order_id
     * @param $type
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function send($order_id, $type)
    {
        if ($type == 1) {
            $order = Db::name('customer_order')->where('id', $order_id)->find();
            $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
            $keyword5 = $course['name'];
        } else {
            $order = Db::name('customer_seat_order')->where('id', $order_id)->find();
            $course = Db::name('customer_house')->where('id', $order['house_id'])->find();
            $keyword5 = $course['name'];
        }
        $cleaning = Db::name('customer_order_cleaning')->where('order_on', $order['order_on'])->find();
        if ($cleaning) {
            $info = Db::name('customer_user')->where('id', $cleaning['user_id'])->find();
            $send_room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
            if ($send_room['province_code']) {


                if ($send_room['province_code']) {
                    $send_room['province_name'] = Db::name('city')->where('citycode', $send_room['province_code'])->find();
                }
                if ($send_room['city_code']) {
                    $send_room['city_name'] = Db::name('city')->where('citycode', $send_room['city_code'])->find();
                }
                if ($send_room['district_code'] > 0) {
                    $send_room['district_name'] = Db::name('city')->where('citycode', $send_room['district_code'])->find();
                    if ($send_room['district_name']) {
                        if ($send_room['district_name'] == "") {
                            $send_room['district_name']['name'] = ' ';
                        }

                    }
                } else {
                    $send_room['district_name']['name'] = ' ';
                }

                $send_room['address_lan'] = $send_room['province_name']['name'];
                if (isset($send_room['province_name']['name'])) {
                    if (isset($send_room['city_name']['name'])) {
                        $send_room['address_lan'] = $send_room['province_name']['name'] . $send_room['city_name']['name'];

                    }
                    if (isset($send_room['district_name']['name'])) {
                        $send_room['address_lan'] = $send_room['province_name']['name'] . $send_room['city_name']['name'] . $send_room['district_name']['name'] . $send_room['address'];

                    }
                }
            }
            $data['thing10'] = $keyword5;
            $data['character_string2'] = $order['order_on'];
            $data['thing11'] = '您好服务还有10分钟结束，请及时清洁';
            $this->send_message_cleaning($data, $info);

        }

    }


    /**
     * 保洁订单通知
     * @param $data
     * @return bool
     */
    public function send_message_cleaning($data, $info)
    {
        $send_message['url'] = '';
        $send_message['thing10']['value'] = $data['thing10'];
        $send_message['character_string2']['value'] = $data['character_string2'];
        $send_message['thing11']['value'] = $data['thing11'];
        return Send::cleaning($send_message, $info);
    }



    /**
     * 玩法未支付
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function seatdeleteOrder()
    {
        $list = Db::name('customer_seat_order')->where('status', 0)->select();
        foreach ($list as $i => $v) {
            if (($v['create_time'] + 120) < time()) {
                Db::name('customer_seat_order')->where('id', $v['id'])->delete();
            }
        }
    }

    /**
     * 玩法结束
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function seatEndOrder()
    {
        $list = Db::name('customer_seat_order')->where('status', 2)->select();
        foreach ($list as $i => $v) {
            if ($v['end_time'] <= time()) {
                $status['status'] = 4;
                Db::name('customer_seat_order')->where('id', $v['id'])->update($status);
                if ($v['house_id'] > 0) {
                    $house_info = Db::name('customer_house')->where('id', $v['house_id'])->find();
                    if ($house_info['door_id']) {
                        $door = Db::name('customer_door_list')->where('id', $house_info['door_id'])->find();
                        $appkey = Db::name('customer_set_cabinet')->where('store_id', $v['store_id'])->find();
                        $door_list_data = $appkey;
                        $door_list_data['equipment_id'] = $door['door_id'];

                        if (true == $this->opens($door_list_data, 04)) {
                            // 记录信息
                            $doorList = array(
                                'admin_id' => $v['store_id'],
                                'create_time' => time(),
                                'user_id' => $v['user_id'],
                                'status' => 1,
                                'order_on' => $v['order_on'],
                                'door_id' => $door['door_id'],
                            );
                            Db::name('mini_admin_lamp_list')->insert($doorList);

                        }
                    }
                }
                $count = "玩法预定";
                $this->distribution($v['store_id'], $v['price'] * 0.15, 3, $count, $v['id']);


                /*$store = Db::name('customer_store_match')->where('store_id', $v['store_id'])->find();
                if ($store) {
                    $v['price'] = $v['price'] * 0.15;
                    $this->SubLedger($v);
                }*/

            } else {
                /** 语音结束播报 */
                if ($v['is_voice_end'] != 1) {
                    if (time() + 60 > $v['end_time']) {

                        $user = Db::name('member')->where('id', $v['user_id'])->find();
                        $seat_list = DB::name('customer_house')->where(['id' => $v['house_id']])->find();
                        $door = DB::name('customer_door_list')->where(['id' => $seat_list['door_id']])->find();

                        if ($door) {
                            $voice = 04;
                            $appkey = Db::name('customer_set_cabinet')->where('store_id', $v['store_id'])->find();
                            if ($appkey) {
                                $door_list_data = $appkey;
                                $door_list_data['equipment_id'] = $door['door_id'];
                                self::openvoice($door_list_data, $v['store_id'], $user['id'], $voice, $v['order_on']);
                            }
                        }
                        $d['is_voice_end'] = 1;
                        $d['update_time'] = time();
                        Db::name('mini_store_seat_order')->where('order_on', $v['order_on'])->update($d);


                    }

                }

                /** 语音播报 */
                if ($v['is_voice'] != 1) {

                    $info = Db::name('coutomer_make')->where('store_id', '=', $v['store_id'])->find();

                    if ($info['is_time'] == 1) {
                        $voice = 02;
                        $end_time = time() + 5 * 60;
                    }
                    if ($info['is_time'] == 2) {
                        $voice = 03;
                        $end_time = time() + 10 * 60;
                    }
                    if ($end_time > $v['end_time']) {
                        $seat_list = DB::name('customer_house')->where(['id' => $v['house_id']])->find();
                        $door = DB::name('customer_door_list')->where(['id' => $seat_list['door_id']])->find();

                        if ($door) {

                            $appkey = Db::name('customer_set_cabinet')->where('store_id', $v['store_id'])->find();
                            if ($appkey) {
                                $door_list_data = $appkey;
                                $door_list_data['equipment_id'] = $door['door_id'];
                                self::openvoice($door_list_data, $v['store_id'], $user['id'], $voice, $v['order_on']);

                            }

                        }

                        $d['is_voice'] = 1;
                        $d['update_time'] = time();
                        Db::name('mini_store_seat_order')->where('order_on', $v['order_on'])->update($d);
                    }
                }

                if (600 + $v['end_time'] > $v['end_time']) {
                    $this->send($v['id'], 1);
                }
            }
        }
    }
    /**
     * 开门
     * @param $door_list
     * @param $admin_id
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public static function newopen($deviceCode, $type)
    {
        header("Content-type: text/html; charset=utf-8");
        $encryptionStr = strtoupper(md5('deviceCode=' . $deviceCode));
        $apiurl = "https://qsg.wuminginfo.com/api/device/generateTask?encryptionStr=" . $encryptionStr . "&deviceCode=" . $deviceCode . "&type=" . $type;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        $data = curl_exec($ch);
        return json_decode($data, true);
    }


}